home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.text;
-
- import java.util.Vector;
-
- public class PlainDocument extends AbstractDocument {
- public static final String tabSizeAttribute = "tabSize";
- public static final String lineLimitAttribute = "lineLimit";
- private AbstractDocument.AbstractElement defaultRoot;
- private Vector added;
- private Vector removed;
-
- public PlainDocument() {
- this(new StringContent());
- }
-
- protected PlainDocument(AbstractDocument.Content c) {
- super(c);
- this.added = new Vector();
- this.removed = new Vector();
- ((AbstractDocument)this).putProperty("tabSize", new Integer(8));
- this.defaultRoot = this.createDefaultRoot();
- }
-
- protected AbstractDocument.AbstractElement createDefaultRoot() {
- AbstractDocument.BranchElement map = new AbstractDocument.BranchElement(this, (Element)null, (AttributeSet)null);
- AbstractDocument.LeafElement line = new AbstractDocument.LeafElement(this, map, (AttributeSet)null, 0, 1);
- Element[] lines = new Element[1];
- lines[0] = line;
- map.replace(0, 0, lines);
- return map;
- }
-
- public Element getDefaultRootElement() {
- return this.defaultRoot;
- }
-
- protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr) {
- this.removed.removeAllElements();
- this.added.removeAllElements();
- AbstractDocument.BranchElement lineMap = (AbstractDocument.BranchElement)this.getDefaultRootElement();
- int offset = chng.getOffset();
- int length = chng.getLength();
- if (offset > 0) {
- --offset;
- ++length;
- }
-
- int index = lineMap.getElementIndex(offset);
- Element rmCandidate = lineMap.getElement(index);
- int rmOffs0 = rmCandidate.getStartOffset();
- int rmOffs1 = rmCandidate.getEndOffset();
- int lastOffset = rmOffs0;
-
- try {
- String str = ((AbstractDocument)this).getText(offset, length);
- boolean hasBreaks = false;
-
- for(int i = 0; i < length; ++i) {
- char c = str.charAt(i);
- if (c == '\n') {
- int breakOffset = offset + i + 1;
- this.added.addElement(new AbstractDocument.LeafElement(this, lineMap, (AttributeSet)null, lastOffset, breakOffset));
- lastOffset = breakOffset;
- hasBreaks = true;
- }
- }
-
- if (hasBreaks) {
- int rmCount = 1;
- this.removed.addElement(rmCandidate);
- if (offset + length == rmOffs1 && lastOffset != rmOffs1 && index + 1 < lineMap.getElementCount()) {
- ++rmCount;
- Element e = lineMap.getElement(index + 1);
- this.removed.addElement(e);
- rmOffs1 = e.getEndOffset();
- }
-
- if (lastOffset < rmOffs1) {
- this.added.addElement(new AbstractDocument.LeafElement(this, lineMap, (AttributeSet)null, lastOffset, rmOffs1));
- }
-
- Element[] aelems = new Element[this.added.size()];
- this.added.copyInto(aelems);
- Element[] relems = new Element[this.removed.size()];
- this.removed.copyInto(relems);
- AbstractDocument.ElementEdit ee = new AbstractDocument.ElementEdit(lineMap, index, relems, aelems);
- chng.addEdit(ee);
- lineMap.replace(index, relems.length, aelems);
- }
-
- } catch (BadLocationException var17) {
- throw new Error("Internal error: " + ((Throwable)var17).toString());
- }
- }
-
- protected void removeUpdate(AbstractDocument.DefaultDocumentEvent chng) {
- this.removed.removeAllElements();
- AbstractDocument.BranchElement map = (AbstractDocument.BranchElement)this.getDefaultRootElement();
- int offset = chng.getOffset();
- int length = chng.getLength();
- int line0 = map.getElementIndex(offset);
- int line1 = map.getElementIndex(offset + length);
- if (line0 != line1) {
- for(int i = line0; i <= line1; ++i) {
- this.removed.addElement(map.getElement(i));
- }
-
- int p0 = map.getElement(line0).getStartOffset();
- int p1 = map.getElement(line1).getEndOffset();
- Element[] aelems = new Element[]{new AbstractDocument.LeafElement(this, map, (AttributeSet)null, p0, p1)};
- Element[] relems = new Element[this.removed.size()];
- this.removed.copyInto(relems);
- AbstractDocument.ElementEdit ee = new AbstractDocument.ElementEdit(map, line0, relems, aelems);
- chng.addEdit(ee);
- map.replace(line0, relems.length, aelems);
- }
-
- }
- }
-